<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TMDT Project Admin</title>
<link rel="stylesheet" href="/admin/resources/css/reset.css"
	type="text/css" media="screen" />
<link rel="stylesheet" href="/admin/resources/css/style.css"
	type="text/css" media="screen" />
<link rel="stylesheet" href="/admin/resources/css/jquery-ui.css"
	type="text/css" media="screen" />
<link rel="stylesheet" href="/admin/resources/css/invalid.css"
	type="text/css" media="screen" />
<script type="text/javascript"
	src="/admin/resources/scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/scripts/jquery-1.8.3.min.js"></script>
<script type="text/javascript"
	src="/admin/resources/scripts/simpla.jquery.configuration.js"></script>
<script type="text/javascript" src="/admin/resources/scripts/facebox.js"></script>
<script type="text/javascript"
	src="/admin/resources/scripts/jquery.wysiwyg.js"></script>
<script type="text/javascript"
	src="/admin/resources/scripts/jquery-ui-1.9.2.custom.js"></script>
<script type="text/javascript"
	src="/admin/resources/scripts/jquery-date-picker.js"></script>

<script>
$('document').ready(function(){
	$('.datepicker').datetimepicker({

		dateFormat: "dd-mm-yy",
		minDate: getFormattedDate(new Date()), 
		timeFormat: "hh:mm:ss"
	});

	function getFormattedDate(date) {
	    var day = date.getDate();
	    var month = date.getMonth() + 1;
	    var year = date.getFullYear().toString().slice(2);
	    return day + '-' + month + '-' + year;
	}}
	);

</script>

</head>
<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->

		<div id="sidebar">
			<div id="sidebar-wrapper">
				<!-- Sidebar with logo and menu -->

				<h1 id="sidebar-title">
					<a href="#">TMDT Project Admin</a>
				</h1>

				<!--  Logo (221px wide) -->
				<a href="#"><img id="logo" src="/admin/resources/images/logo.png"
					alt="Simpla Admin logo" /></a>

			
			<?php include_once 'create_auction_main_nav.php'?>
			

			
		</div>
		</div>
		<!-- End #sidebar -->
		<div id="clear"></div>
		<div id="main-content">
			<!-- Main Content Section with everything -->
	<?php include_once '../shortcut_buttons.php'?>
		<?php
		include_once '../config.php'?>
<noscript>
				<!-- Show a notification if the user has disabled javascript -->
				<div class="notification error png_bg">
					<div>
						Javascript is disabled or is not supported by your browser. Please
						<a href="http://browsehappy.com/"
							title="Upgrade to a better browser">upgrade</a> your browser or <a
							href="http://www.google.com/support/bin/answer.py?answer=23852"
							title="Enable Javascript in your browser">enable</a> Javascript
						to navigate the interface properly.
					</div>
				</div>
			</noscript>


	<?php
	include_once '/pages/config.php';
	$nameErr = $productIDErr = $quantityErr = $end_timeErr = "";
	$auction_name = $product_id = $quantity = $start_time = $end_time = $upset_price = $price_per_bid = "";
	$message = "";
	$err = false;
	
	if ($_SERVER ["REQUEST_METHOD"] == "POST") {
		if (empty ( $_POST ["auction_name"] )) {
			$nameErr = "Auction name is required.";
			$err = true;
		} else {
			$auction_name = trim ( $_POST ["auction_name"] );
		}
		
		if (empty ( $_POST ["product_id"] )) {
			$productIDErr = "Product ID is required.";
			$err = true;
		} else {
			$product_id = trim ( $_POST ["product_id"] );
		}
		
		if (empty ( $_POST ["quantity"] )) {
			$quantityErr = "Quantity is required.";
			$err = true;
			
		} else {
			$quantity = trim ( $_POST ["quantity"] );
		}
		
		if (empty ( $_POST ["start_time"] ) || trim ( strtoupper ( $_POST ["start_time"] ) == "NOW" )) {
			$start_time = "NOW";
		} else {
			$start_time = trim ( $_POST ["start_time"] );
		}
		
		if (empty ( $_POST ["end_time"] )) {
			$end_timeErr = "End time is required.";
			$err = true;
		} else {
			$end_time = trim ( $_POST ["end_time"] );
		}
		
		if (empty ( $_POST ["upset_price"] ) || trim ( strtoupper ( $_POST ["upset_price"] ) == "DEFAULT" )) {
			$upset_price = 0;
		} else {
			$upset_price = trim ( $_POST ["upset_price"] );
		}
		
		if (empty ( $_POST ["price_per_bid"] ) || trim ( strtoupper ( $_POST ["price_per_bid"] ) == "DEFAULT" )) {
			$price_per_bid = "DEFAULT";
		} else {
			$price_per_bid = trim ( $_POST ["price_per_bid"] );
		}
		
		// ////////////////////////////////////////////////
		// Insert Auction into Database
		// ////////////////////////////////////////////////
		if ($err == true) {
			?>
				
				<div class="notification error png_bg">
				<a href="#" class="close"><img
					src="/admin/resources/images/icons/cross_grey_small.png"
					title="Close this notification" alt="close" /></a>
				<div>
						Validate Error: <?php echo "$nameErr  $productIDErr  $quantityErr  $end_timeErr"?>	
					</div>
			</div>	

				<?php
		} else {
			
			$connection = mysql_connect ( $host, $username, $password ) or die ( "Could not connect to MySql" . mysql_error () );
			
			mysql_select_db ( $db, $connection ) or die ( "Could not connect to MySql" . mysql_error () );
			if ($start_time == "NOW") {
				$statement = "INSERT INTO Auction(name,productID,quantity,end_time,auction_price,price_per_bid) 
					VALUES ('$auction_name','$product_id',$quantity,STR_TO_DATE('$end_time','%d-%m-%Y %H:%i:%s'),$upset_price,$price_per_bid )";
			} else {
				$statement = "INSERT INTO Auction(name,productID,quantity,start_time,end_time,auction_price,price_per_bid) 
					VALUES ('$auction_name','$product_id',$quantity,STR_TO_DATE('$start_time','%d-%m-%Y %H:%i:%s'),STR_TO_DATE('$end_time','%d-%m-%Y %H:%i:%s'),$upset_price,$price_per_bid )";
			}
			$result = mysql_query ( $statement );
			
			if (! $result) {
				$message = "MySQL: Error " . mysql_errno () . " : " . mysql_error ();
				?>
				
				<div class="notification error png_bg">
				<a href="#" class="close"><img
					src="/admin/resources/images/icons/cross_grey_small.png"
					title="Close this notification" alt="close" /></a>
				<div>
						<?php echo $statement."  ".$message; ?>	
					</div>
			</div>	

				<?php
			} else {
				$message = "Insert Aution Successful";
				?>
			<div class="notification success png_bg">
				<a href="#" class="close"><img
					src="/admin/resources/images/icons/cross_grey_small.png"
					title="Close this notification" alt="close" /></a>
				<div>
					<?php echo $message; ?>	
				</div>
			</div>

				<?php
			}
			mysql_close ( $connection );
		}
	}
	?>

			

			

			<div class="clear"></div>
			<!-- End .clear -->
			<div class="content-box">
				<!-- Start Content Box -->

				<div class="content-box-header">

					<h3>Auctions</h3>

					<ul class="content-box-tabs">

						<li><a href="#create_auction_tab">Create New Auction</a></li>
					</ul>


					<div class="clear"></div>
					<!-- End .clear -->
				</div>
				<!-- End .content-box-header -->

				<div class="content-box-content">

					<div class="tab-content default-tab" id="create_auction_tab">

						<form
							action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
							method="post">

							<fieldset>
								<!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->
<?php
$connection = mysql_connect ( $host, $username, $password ) or die ( "Could not connect to MySql" . mysql_error () );
mysql_select_db ( $db, $connection ) or die ( "Could not connect to MySql" . mysql_error () );

?>
<p>
									<label>Auction Name(*)</label> <input
										class="text-input medium-input" type="text"
										id="auction_name" name="auction_name"
										value="<?php echo htmlspecialchars($auction_name);?>" /> <span
										id="auction_name_message" class="validate_err"><?php echo $nameErr?></span>
									<!-- Classes for input-notification: success, error, information, attention -->
									<br /> <small>Name for this auction</small>
								</p>
								<p>
									<label>Product </label> <select class="small-input"
										name="product_id" id="product_id">

		<?php
		$result = mysql_query ( "select name,productID from Product" );
		
		while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) {
			
			?>
		<option value=<?php echo htmlspecialchars("'".$row["productID"]."'")?>><?php echo $row["name"]?></option>
		<?php
		}
		mysql_close ( $connection );
		?>
	</select> <span id="product_id_message" class="validate_err"><?php echo $productIDErr?></span>
								</p>


								<p>
									<label>Quantity(*)</label> <input
										class="text-input small-input" type="text" id="quantity"
										name="quantity"
										value="<?php echo htmlspecialchars($quantity);?>" /> <span
										id="quantity_message" class="validate_err"><?php echo htmlspecialchars($quantityErr);?></span>
									<!-- Classes for input-notification: success, error, information, attention -->
									<br /> <small>Number of products are avaiable for this auction</small>
								</p>
								<p>
									<label>Start Time</label> <input
										class="text-input small-input datepicker" type="text"
										id="start_time" name="start_time"
										value="<?php echo htmlspecialchars($start_time);?>" /> <span
										id="start_time_message"></span> <br /> <small>The auction will
										start at this time</small> <br /> <small>Format: DD-MM-YYYY
										HH:MM:SS. Default: Current Date-time</small>
								</p>

								<p>
									<label>End Time(*)</label> <input
										class="text-input small-input datepicker" type="text"
										id="end_time" name="end_time"
										value="<?php echo htmlspecialchars($end_time);?>" /> <span
										id="end_time_message" class="validate_err"><?php echo $end_timeErr?></span>
									<br /> <small>The auction will be closed at this time</small> <br />
									<small>Format: DD-MM-YYYY HH:MM:SS.</small>
								</p>

								<p>
									<label>Upset Price</label> <input
										class="text-input small-input" type="text" id="upset_price"
										name="upset_price"
										value="<?php echo htmlspecialchars($upset_price);?>" /> <span
										id="upset_price_message"></span>
									<!-- Classes for input-notification: success, error, information, attention -->
									<br /> <small>The upset price (beginning price) for this
										auction. Default: 0</small>
								</p>

								<p>
									<label>Price per Bid</label> <input
										class="text-input small-input" type="text" id="price_per_bid"
										name="price_per_bid"
										value="<?php echo htmlspecialchars($price_per_bid);?>" /> <span
										id="price_per_bid_message"></span>
									<!-- Classes for input-notification: success, error, information, attention -->
									<br /> <small>This value will be add to the auction price after
										one bid from user. Default: 1000</small>
								</p>


								<p>
									<input class="button" type="submit" value="Create" /> <a
										href="/admin/index.php"><input class="button" value="Cancel" />
									</a>
								</p>

							</fieldset>

							<div class="clear"></div>
							<!-- End .clear -->

						</form>

					</div>
					<!-- End #tab2 -->
				</div>
				<!-- End .content-box-content -->
			</div>
			<!-- End .content-box -->


			<div class="clear"></div>

			<?php /*include_once 'notifications.php'*/?>
			<?php /*include_once 'footer.php'*/?>
			

			
			

			
		</div>
		<!-- End #main-content -->
	</div>
</body>
</html>

